/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlutils;

import DTO.album.AlbumType;
import DTO.album.Albums;
import java.math.BigInteger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DinhNP
 */
public class SearchHotAlbumsSAXHandler extends DefaultHandler{

    private Albums albumList;
    private String tagName;
    private AlbumType temp;

    public SearchHotAlbumsSAXHandler() {
        albumList = new Albums();
        tagName = "";
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tagName = qName;

        if (qName.equals("AlbumID")){
            temp = new AlbumType();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("AlbumID")){
            if (temp!=null){
                albumList.getAlbum().add(temp);
            }
        }
        tagName = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String content = new String(ch, start, length);
        if (tagName.equals("AlbumID")){
            temp.setAlbumID(BigInteger.valueOf(Integer.parseInt(content.trim())));
        }else if (tagName.equals("AlbumTitle")){
            temp.setAlbumTitle(content.toString().trim());
        }else if (tagName.equals("ArtistID")){
            temp.setArtistID(BigInteger.valueOf(Integer.parseInt(content.trim())));
        }else if (tagName.equals("ArtistName")){
            temp.setArtistName(content.toString().trim());
        }else if (tagName.equals("AlbumCoverSRC")){
            temp.setAlbumCoverSRC(content.toString().trim());
        }else if (tagName.equals("AlbumViews")){
            temp.setAlbumViews(BigInteger.valueOf(Integer.parseInt(content.trim())));
        }
    }

    public Albums getAlbumList() {
        return albumList;
    }
}
